//
//  MoneyLogAppDelegate.m
//  MoneyLog
//
//  Created by Max on 11-1-20.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MoneyLogAppDelegate.h"

#import "SQLiteHelper.h"
#import "offlineAlertViewController.h"
#import "Reachability.h"

#define kHostName @"www.wicresoft.com"

@implementation MoneyLogAppDelegate

@synthesize window; 
@synthesize tabBarController;
@synthesize navigationController;

#pragma mark -
#pragma mark Application lifecycle

- (void) updateInterfaceWithReachability: (Reachability*) curReach
{
    //NetworkStatus netStatus = [curReach currentReachabilityStatus];
    BOOL connectionRequired= [curReach connectionRequired];
    if(connectionRequired)
    {
//        UIAlertView *alertView=[[UIAlertView alloc] 
//                                initWithTitle:@"提示" 
//                                message:@"哎，网络出问题了！" 
//                                delegate:nil 
//                                cancelButtonTitle:@"确定" 
//                                otherButtonTitles:nil, nil];
//        [alertView show];
//        [alertView release];
        
       /* CGRect appRect=[[UIScreen mainScreen] applicationFrame];
        UIView *view=[[UIView alloc] initWithFrame:CGRectMake((appRect.size.width-100)/2, (appRect.size.height-100)/2, 100, 100)];
        [view setAlpha:0.3];
        
        UIImage *image=[[UIImage alloc] initWithContentsOfFile:@"system.png"];
        UIImageView *imageView = [[UIImage alloc] init];
        [imageView initWithImage:image];
        
        [view addSubview:imageView];
        [image release];
        [imageView release];
        
        UILabel *textLable=[[UILabel alloc] initWithFrame:CGRectMake(10, 80, 80, 80)];
        textLable.text=@"问题！";
        [view addSubview:textLable];
        [textLable release];
        
        [window addSubview:view];
        [view release];
        
        offlineAlertViewController *alertView=[[offlineAlertViewController alloc] init];
        [window addSubview:alertView.view];
        [alertView release];*/
    }
}

//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note
{
	Reachability* curReach = [note object];
	NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
	[self updateInterfaceWithReachability: curReach];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    // Override point for customization after application launch.
	
	[Database initialize];
    
    // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the
    // method "reachabilityChanged" will be called. 
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
    
    hostReach = [[Reachability reachabilityWithHostName: @"www.wicresoft.com"] retain];
	[hostReach startNotifier];
	[self updateInterfaceWithReachability: hostReach];

	//rootViewController *controller=[[rootViewController alloc] init];
	//navigationController =[[UINavigationController alloc] initWithRootViewController:controller];
	//[window addSubview:navigationController.view];
    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible]; 
	
	//[controller release];
 	
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
     */
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    /*
     Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
     */
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}


- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
    [g_Database close];
    [hostReach stopNotifier];
}


#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    /*
     Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
     */
}


- (void)dealloc {
    [tabBarController release];
	[navigationController release];
    [window release];
    [super dealloc];
}


@end
